package com.shawn.algorithm.recursion;

import java.util.HashMap;
import java.util.Map;

public class Fibonacci {

	private Map<Integer, Integer> map = new HashMap<>();

	private int count;

	public int f(int n) {
		count++;
		if (count > 1000) {
			throw new RuntimeException("stack depth: " + count);
		}
		if (n == 0) {
			return 1;
		}
		if (n == 1) {
			return 1;
		}
		Integer value = map.get(n);
		if (value != null) {
			return value;
		}
		map.put(n, f(n - 1) + f(n - 2));

		return map.get(n);
	}

	public static void main(String[] args) {
		Fibonacci f = new Fibonacci();
		System.out.println(f.f(4));
	}

}
